import type { DirectiveBinding, ObjectDirective } from "vue";

const doubleClick: ObjectDirective = {
  mounted(el: HTMLElement, binding: DirectiveBinding) {
    const { value } = binding;
    let timeoutId: number;

    el.addEventListener("click", () => {
      if (timeoutId) {
        clearTimeout(timeoutId);
        timeoutId = 0;
        value();
      } else {
        timeoutId = setTimeout(() => {
          timeoutId = 0;
        }, 300);
      }
    });
  },
};

export default doubleClick